package com.ctc.lottery.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ctc.lottery.common.BaseContext;
import com.ctc.lottery.entity.Admin;
import com.ctc.lottery.service.AdminService;
import com.ctc.lottery.service.LotteryService;
import com.ctc.lottery.vo.LotteryVo;
import com.ctc.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Api(tags = "管理员")
@RestController
@RequestMapping(value = "/admin")
@Slf4j
public class AdminController {

    @Autowired
    private LotteryService lotteryService;
    @Autowired
    private AdminService adminService;
    @Autowired
    private StringRedisTemplate redisTemplate;

    @ApiOperation(value = "管理员登录")
    @PostMapping(value = "/login")
    public R login(String username, String password) {
        Admin admin = adminService.getOne(
                new LambdaQueryWrapper<Admin>().eq(Admin::getUsername, username).eq(Admin::getPassword, password)
        );
        if (admin==null){
            return R.error().put("data","登录失败，用户名或密码错误");
        }
        BaseContext.setCurrentUserName(username);
        String uuid = "uesr-token:"+ UUID.randomUUID();
        redisTemplate.opsForValue().set(uuid,username,30, TimeUnit.MINUTES);
        return R.ok().put("data",uuid);
    }


    @ApiOperation(value = "设置抽奖活动")
    @PostMapping(value = "/setLottery")
    public R setLottery(@RequestBody LotteryVo vo) {
        lotteryService.setLottery(vo);
        return R.ok();
    }
}
